#!/usr/bin/env bash
set -ex

cd $(dirname $0)/..

source ./scripts/version.sh

if [ "${GOARCH}" == "s390x" ] || [ "${GOARCH}" == "arm64" ]; then
    exit 0
fi

if [ -z "${GODEBUG}" ]; then
    EXTRA_LDFLAGS="${EXTRA_LDFLAGS} -w"
    DEBUG_GO_GCFLAGS=""
    DEBUG_TAGS=""
else
    DEBUG_GO_GCFLAGS='-gcflags=all=-N -l'
fi

REVISION=$(git rev-parse HEAD)$(if ! git diff --no-ext-diff --quiet --exit-code; then echo .dirty; fi)
GOOS=windows
RELEASE=${PROG}.${GOOS}-${GOARCH}


BUILDTAGS="netgo osusergo no_stage static_build sqlite_omit_load_extension no_cri_dockerd"
GO_BUILDTAGS="${GO_BUILDTAGS} ${BUILDTAGS} ${DEBUG_TAGS}"

VERSION_FLAGS="
    -X ${K3S_PKG}/pkg/version.GitCommit=${REVISION}
    -X ${K3S_PKG}/pkg/version.Program=${PROG}
    -X ${K3S_PKG}/pkg/version.Version=${VERSION}
    -X ${K3S_PKG}/pkg/version.UpstreamGolang=${VERSION_GOLANG}
    -X ${RKE2_PKG}/pkg/images.DefaultRegistry=${REGISTRY}
    -X ${RKE2_PKG}/pkg/images.DefaultEtcdImage=rancher/hardened-etcd:${ETCD_VERSION}-build20250612
    -X ${RKE2_PKG}/pkg/images.DefaultKubernetesImage=${REPO}/hardened-kubernetes:${KUBERNETES_IMAGE_TAG}
    -X ${RKE2_PKG}/pkg/images.DefaultPauseImage=rancher/mirrored-pause:${PAUSE_VERSION}
    -X ${RKE2_PKG}/pkg/images.DefaultRuntimeImage=${REPO}/${PROG}-runtime:${DOCKERIZED_VERSION}-${GOOS}-${GOARCH}
"

GO_LDFLAGS="${STATIC_FLAGS} ${EXTRA_LDFLAGS}"
echo ${DEBUG_GO_GCFLAGS}
GOOS=windows \
    CGO_ENABLED=1 CGO_CFLAGS="-DSQLITE_ENABLE_DBSTAT_VTAB=1 -DSQLITE_USE_ALLOCA=1" CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc \
    go build \
        -tags "${GO_BUILDTAGS}" \
        ${GO_GCFLAGS} ${GO_BUILD_FLAGS} \
        -o bin/${PROG}.exe \
        -ldflags "${GO_LDFLAGS} ${VERSION_FLAGS}" \
        ${GO_TAGS}
